//
//  HWQQGameTypeHeader.swift
//  HWanGame
//
//  Created by 王昌健 on 2023/6/5.
//  Copyright © 2023 94hwan. All rights reserved.
//

import UIKit

class HWQQGameTypeHeader: UITableViewHeaderFooterView {
    var bgImageView:UIImageView!
    var firstBgImageView:UIImageView!
    var secondBgView:UIView!
    var thirdBgView:UIView!
    var firstCrown:UIImageView!
    var secondCrown:UIImageView!
    var thirdCrown:UIImageView!
    var firstNum:UIImageView!
    var secondNum:UIImageView!
    var thirdNum:UIImageView!
    var firstLogoImage:UIImageView!
    var secondLogoImage:UIImageView!
    var thirdLogoImage:UIImageView!
    var firstNameLabel:UILabel!
    var secondNameLabel:UILabel!
    var thirdNameLabel:UILabel!
    var firstContentLabel:UILabel!
    var secondContentLabel:UILabel!
    var thirdContentLabel:UILabel!
    var firstOpenLabel:UILabel!
    var secondOpenLabel:UILabel!
    var thirdOpenLabel:UILabel!
    var firstButton:UIButton!
    var secondButton:UIButton!
    var thirdButton:UIButton!
    var firstShadowView:UIView!
    var secondShadowView:UIView!
    var thirdShadowView:UIView!
    
    var models:[HWQQGameListModel]?{
        didSet {
            firstBgImageView.isHidden = true
            secondBgView.isHidden = true
            thirdBgView.isHidden = true
            
            if (models?.count ?? 0) > 0 {
                firstBgImageView.isHidden = false
                firstLogoImage.kf.setImage(with: URL(string: models?[0].icon ?? ""))
                firstNameLabel.text = models![0].name
                firstContentLabel.text = models![0].content
            }
            
            if (models?.count ?? 0) > 1 {
                secondBgView.isHidden = false
                secondLogoImage.kf.setImage(with: URL(string: models?[1].icon ?? ""))
                secondNameLabel.text = models![1].name
                secondContentLabel.text = models![1].content
            }
            
            if (models?.count ?? 0) > 2 {
                thirdBgView.isHidden = false
                thirdLogoImage.kf.setImage(with: URL(string: models?[2].icon ?? ""))
                thirdNameLabel.text = models![2].name
                thirdContentLabel.text = models![2].content
            }
        }
    }
    
    class func headerOrFooterWithTableView(tableView:UITableView) -> (HWQQGameTypeHeader) {
        let reUse:String = "HWQQGameTypeHeader"
        
        var headerOrFooter = tableView.dequeueReusableHeaderFooterView(withIdentifier: reUse)
        
        if headerOrFooter == nil {
            headerOrFooter = HWQQGameTypeHeader.init(reuseIdentifier: reUse)
        }
        
        return headerOrFooter as! HWQQGameTypeHeader
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(reuseIdentifier: String?) {
        super.init(reuseIdentifier: reuseIdentifier)
        initUI()
        initFrame()
    }
    
    
    func initUI() -> Void {
        
        backgroundView = UIView()
        
        bgImageView = UIImageView()
        bgImageView.image = UIImage(named: "qqgameHeaderBg")
        contentView.addSubview(bgImageView)
        
        firstBgImageView = UIImageView()
        firstBgImageView.image = UIImage(named: "qqGameFirstBgImg")
        bgImageView.addSubview(firstBgImageView)
        
        secondBgView = UIView()
        secondBgView.backgroundColor = .clear
        bgImageView.addSubview(secondBgView)
        
        thirdBgView = UIView()
        thirdBgView.backgroundColor = .clear
        bgImageView.addSubview(thirdBgView)
        
        firstCrown = UIImageView()
        firstCrown.image = UIImage(named: "firstCrown")
        firstBgImageView.addSubview(firstCrown)
        
        firstNum = UIImageView()
        firstNum.image = UIImage(named: "qqGameFirst")
        firstBgImageView.addSubview(firstNum)
        
        firstShadowView = UIView()
        firstShadowView.backgroundColor = .white
        firstShadowView.layer.cornerRadius = 18*widthScale
        firstShadowView.layer.shadowColor = UIColor.colorWithAlphaHexString(hexString: "#000000", alpha: 0.3).cgColor
        firstShadowView.layer.shadowOffset = CGSize(width: 0, height: 4*widthScale)
        firstShadowView.layer.shadowRadius = 5
        firstShadowView.layer.shadowOpacity = 1
        firstBgImageView.addSubview(firstShadowView)
        
        firstLogoImage = UIImageView()
        firstLogoImage.layer.cornerRadius = 18*widthScale
        firstLogoImage.layer.masksToBounds = true
        firstBgImageView.addSubview(firstLogoImage)
        
        firstNameLabel = HWLabel.createLabel(title: "", color: "#333333", font: 13*widthScale)
        firstNameLabel.textAlignment = .center
        firstBgImageView.addSubview(firstNameLabel)
        
        firstContentLabel = HWLabel.createLabel(title: "", color: "#333333", font: 11*widthScale)
        firstContentLabel.textAlignment = .center
        firstBgImageView.addSubview(firstContentLabel)
        
        firstOpenLabel = HWLabel.createLabel(title: "打开", color: "#FF8B00", font: 13*widthScale)
        firstOpenLabel.textAlignment = .center
        firstOpenLabel.layer.cornerRadius = 14*widthScale
        firstOpenLabel.layer.borderColor = UIColor.colorWithHexString(hexString: "#FF8B00").cgColor
        firstOpenLabel.layer.borderWidth = 1*widthScale
        firstBgImageView.addSubview(firstOpenLabel)
        
        firstButton = UIButton(type: .custom)
        firstBgImageView.addSubview(firstButton)
        
        secondCrown = UIImageView()
        secondCrown.image = UIImage(named: "secondCrown")
        secondBgView.addSubview(secondCrown)
        
        secondNum = UIImageView()
        secondNum.image = UIImage(named: "qqGameSecond")
        secondBgView.addSubview(secondNum)
        
        secondShadowView = UIView()
        secondShadowView.backgroundColor = .white
        secondShadowView.layer.cornerRadius = 18*widthScale
        secondShadowView.layer.shadowColor = UIColor.colorWithAlphaHexString(hexString: "#000000", alpha: 0.3).cgColor
        secondShadowView.layer.shadowOffset = CGSize(width: 0, height: 4*widthScale)
        secondShadowView.layer.shadowRadius = 5
        secondShadowView.layer.shadowOpacity = 1
        secondBgView.addSubview(secondShadowView)
        
        secondLogoImage = UIImageView()
        secondLogoImage.layer.cornerRadius = 18*widthScale
        secondLogoImage.layer.masksToBounds = true
        secondBgView.addSubview(secondLogoImage)
        
        secondNameLabel = HWLabel.createLabel(title: "", color: "#333333", font: 13*widthScale)
        secondNameLabel.textAlignment = .center
        secondBgView.addSubview(secondNameLabel)
        
        secondContentLabel = HWLabel.createLabel(title: "", color: "#333333", font: 11*widthScale)
        secondContentLabel.textAlignment = .center
        secondBgView.addSubview(secondContentLabel)
        
        secondOpenLabel = HWLabel.createLabel(title: "打开", color: "#FF8B00", font: 13*widthScale)
        secondOpenLabel.textAlignment = .center
        secondOpenLabel.layer.cornerRadius = 14*widthScale
        secondOpenLabel.layer.borderColor = UIColor.colorWithHexString(hexString: "#FF8B00").cgColor
        secondOpenLabel.layer.borderWidth = 1*widthScale
        secondBgView.addSubview(secondOpenLabel)
        
        secondButton = UIButton(type: .custom)
        secondBgView.addSubview(secondButton)
        
        thirdCrown = UIImageView()
        thirdCrown.image = UIImage(named: "thirdCrown")
        thirdBgView.addSubview(thirdCrown)
        
        thirdNum = UIImageView()
        thirdNum.image = UIImage(named: "qqGameThird")
        thirdBgView.addSubview(thirdNum)
        
        thirdShadowView = UIView()
        thirdShadowView.backgroundColor = .white
        thirdShadowView.layer.cornerRadius = 18*widthScale
        thirdShadowView.layer.shadowColor = UIColor.colorWithAlphaHexString(hexString: "#000000", alpha: 0.3).cgColor
        thirdShadowView.layer.shadowOffset = CGSize(width: 0, height: 4*widthScale)
        thirdShadowView.layer.shadowRadius = 5
        thirdShadowView.layer.shadowOpacity = 1
        thirdBgView.addSubview(thirdShadowView)
        
        thirdLogoImage = UIImageView()
        thirdLogoImage.layer.cornerRadius = 18*widthScale
        thirdLogoImage.layer.masksToBounds = true
        thirdBgView.addSubview(thirdLogoImage)
        
        thirdNameLabel = HWLabel.createLabel(title: "", color: "#333333", font: 13*widthScale)
        thirdNameLabel.textAlignment = .center
        thirdBgView.addSubview(thirdNameLabel)
        
        thirdContentLabel = HWLabel.createLabel(title: "", color: "#333333", font: 11*widthScale)
        thirdContentLabel.textAlignment = .center
        thirdBgView.addSubview(thirdContentLabel)
        
        thirdOpenLabel = HWLabel.createLabel(title: "打开", color: "#FF8B00", font: 13*widthScale)
        thirdOpenLabel.textAlignment = .center
        thirdOpenLabel.layer.cornerRadius = 14*widthScale
        thirdOpenLabel.layer.borderColor = UIColor.colorWithHexString(hexString: "#FF8B00").cgColor
        thirdOpenLabel.layer.borderWidth = 1*widthScale
        thirdBgView.addSubview(thirdOpenLabel)
        
        thirdButton = UIButton(type: .custom)
        thirdBgView.addSubview(thirdButton)
        
    }
    
    func initFrame() -> Void {
        bgImageView.snp.makeConstraints { make in
            make.edges.equalToSuperview().priority(.high)
        }
        
        firstBgImageView.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.bottom.equalToSuperview()
            make.width.equalTo(121*widthScale)
            make.height.equalTo(230*widthScale)
        }
        
        secondBgView.snp.makeConstraints { make in
            make.left.top.bottom.equalToSuperview()
            make.right.equalTo(firstBgImageView.snp.left)
        }
        
        thirdBgView.snp.makeConstraints { make in
            make.right.bottom.top.equalToSuperview()
            make.left.equalTo(firstBgImageView.snp.right)
        }
        
        firstCrown.snp.makeConstraints { make in
            make.left.equalTo(14*widthScale)
            make.top.equalTo(19*widthScale)
            make.width.equalTo(36*widthScale)
            make.height.equalTo(34*widthScale)
        }
        
        firstNum.snp.makeConstraints { make in
            make.right.equalTo(-9*widthScale)
            make.top.equalTo(-4*widthScale)
            make.width.equalTo(36*widthScale)
            make.height.equalTo(49*widthScale)
        }
        
        firstShadowView.snp.makeConstraints { make in
            make.top.equalTo(38*widthScale)
            make.width.height.equalTo(72*widthScale)
            make.centerX.equalToSuperview()
        }
        
        firstLogoImage.snp.makeConstraints { make in
            make.top.equalTo(38*widthScale)
            make.width.height.equalTo(72*widthScale)
            make.centerX.equalToSuperview()
        }
        
        firstNameLabel.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(firstLogoImage.snp.bottom).offset(13*widthScale)
            make.height.equalTo(13*widthScale)
        }
        
        firstContentLabel.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(firstNameLabel.snp.bottom).offset(6*widthScale)
            make.height.equalTo(13*widthScale)
        }
        
        firstOpenLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(firstContentLabel.snp.bottom).offset(19*widthScale)
            make.height.equalTo(28*widthScale)
            make.width.equalTo(60*widthScale)
        }
        
        firstButton.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        secondCrown.snp.makeConstraints { make in
            make.left.equalTo(19*widthScale)
            make.top.equalTo(56*widthScale)
            make.width.equalTo(36*widthScale)
            make.height.equalTo(34*widthScale)
        }
        
        secondNum.snp.makeConstraints { make in
            make.right.equalTo(-8*widthScale)
            make.top.equalTo(34*widthScale)
            make.width.equalTo(37*widthScale)
            make.height.equalTo(49*widthScale)
        }
        
        secondShadowView.snp.makeConstraints { make in
            make.top.equalTo(76*widthScale)
            make.width.height.equalTo(72*widthScale)
            make.left.equalTo(31*widthScale)
        }
        
        secondLogoImage.snp.makeConstraints { make in
            make.top.equalTo(76*widthScale)
            make.width.height.equalTo(72*widthScale)
            make.left.equalTo(31*widthScale)
        }
        
        secondNameLabel.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(secondLogoImage.snp.bottom).offset(12*widthScale)
            make.height.equalTo(13*widthScale)
        }
        
        secondContentLabel.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(secondNameLabel.snp.bottom).offset(6*widthScale)
            make.height.equalTo(13*widthScale)
        }
        
        secondOpenLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(secondContentLabel.snp.bottom).offset(11*widthScale)
            make.height.equalTo(28*widthScale)
            make.width.equalTo(60*widthScale)
        }
        
        secondButton.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        thirdCrown.snp.makeConstraints { make in
            make.left.equalTo(12*widthScale)
            make.top.equalTo(57*widthScale)
            make.width.equalTo(36*widthScale)
            make.height.equalTo(34*widthScale)
        }
        
        thirdNum.snp.makeConstraints { make in
            make.right.equalTo(-10*widthScale)
            make.top.equalTo(34*widthScale)
            make.width.equalTo(36*widthScale)
            make.height.equalTo(49*widthScale)
        }
        
        thirdShadowView.snp.makeConstraints { make in
            make.top.equalTo(76*widthScale)
            make.width.height.equalTo(72*widthScale)
            make.left.equalTo(24*widthScale)
        }
        
        thirdLogoImage.snp.makeConstraints { make in
            make.top.equalTo(76*widthScale)
            make.width.height.equalTo(72*widthScale)
            make.left.equalTo(24*widthScale)
        }
        
        thirdNameLabel.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(thirdLogoImage.snp.bottom).offset(12*widthScale)
            make.height.equalTo(13*widthScale)
        }
        
        thirdContentLabel.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(thirdNameLabel.snp.bottom).offset(6*widthScale)
            make.height.equalTo(13*widthScale)
        }
        
        thirdOpenLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(thirdContentLabel.snp.bottom).offset(11*widthScale)
            make.height.equalTo(28*widthScale)
            make.width.equalTo(60*widthScale)
        }
        
        thirdButton.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
    }
}
